package com.leetcode.partition8;

/**
 * @author `RKC`
 * @date 2021/11/1 17:24
 */
public class LC724寻找数组的中心下标 {

    public static int pivotIndex(int[] nums) {
        return prefixSum(nums);
    }

    public static void main(String[] args) {
//        int[] nums = {1, 7, 3, 6, 5, 6};
//        int[] nums = {1, 2, 3};
        int[] nums = {2, 1, -1};
        System.out.println(pivotIndex(nums));
    }

    private static int prefixSum(int[] nums) {
        int[] prefix = new int[nums.length + 1];
        for (int i = 1; i <= nums.length; i++) prefix[i] = prefix[i - 1] + nums[i - 1];
        for (int i = 1; i < prefix.length; i++) {
            int left = prefix[i - 1], right = prefix[prefix.length - 1] - prefix[i];
            if (left == right) {
                return i - 1;
            }
        }
        return -1;
    }
}
